from datetime import datetime, timezone
from app.extensions import db

class Transaction(db.Model):
    __tablename__ = 'transactions'
    id = db.Column(db.BigInteger, primary_key=True, autoincrement=True, comment='记录ID')
    user_id = db.Column(db.BigInteger, db.ForeignKey('user.user_id'), nullable=False, comment='关联用户ID')
    type = db.Column(db.Enum('expense', 'income'), nullable=False, comment='类型（支出/收入）')
    category_id = db.Column(db.BigInteger, db.ForeignKey('categories.id'), nullable=False, comment='关联分类ID')
    account = db.Column(db.String(50), nullable=False, comment='账户（如微信、现金、银行卡）') 
    amount = db.Column(db.Numeric(10, 2), nullable=False, comment='金额（保留2位小数）')
    remark = db.Column(db.String(200), comment='备注')
    date = db.Column(db.DateTime, nullable=False, comment='交易时间')
    created_at = db.Column(db.DateTime, nullable=False, default=lambda: datetime.now(timezone.utc), comment='创建时间')
    updated_at = db.Column(db.DateTime, nullable=False, default=lambda: datetime.now(timezone.utc), onupdate=lambda: datetime.now(timezone.utc), comment='更新时间')

    # 关联用户
    user = db.relationship('User', backref='transactions', lazy=True)
    category = db.relationship('Category', back_populates='transactions', lazy=True)